package com.example.Arithmetic.Lettcode;

import org.junit.jupiter.api.Test;

import java.util.*;

/**
 * 日期：2023/12/25
 * 时间：20:53
 * 描述：力扣49题
 * 找出相同重排序的字符串，并分组
 * 解题：先给字符串排序，从map中获取，获取不到新增一个分组
 */
public class E01Lettcode49_1 {
    static class Array {
        int[] key=new int[26];

            @Override
            public boolean equals(Object o) {
                if (this == o) return true;
                if (o == null || getClass() != o.getClass()) return false;
                Array array = (Array) o;
                return Arrays.equals(key, array.key);
            }

            @Override
            public int hashCode() {
                return Arrays.hashCode(key);
            }
        public Array(String s){
                for (int i = 0; i < s.length(); i++) {
                    char c = s.charAt(i);
                    key[c-97]++;
                }
            }
        }

        public List<List<String>> groupAnagrams(String[] strs) {
            Map<Array,List<String>> map=new HashMap();
            for (String s:strs){
            var list = map.computeIfAbsent(new Array(s), e -> new ArrayList<>());
            list.add(s);
        }
        return new ArrayList<>(map.values());
    }

    @Test
    public void tets() {
        String[] s = {"eat", "tea", "tan", "ate", "nat", "bat"};
        var lists = groupAnagrams(s);
        System.out.println(lists);
    }
}

